package cn.yuanqiao.archive.feedbackEvaluation.service.impl;

import cn.yuanqiao.archive.appManager.domain.DataScopeBase;
import cn.yuanqiao.archive.feedbackEvaluation.domain.FeedbackEvaluation;
import cn.yuanqiao.archive.feedbackEvaluation.mapper.FeedbackEvaluationMapper;
import cn.yuanqiao.archive.feedbackEvaluation.service.IFeedbackEvaluationService;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 反馈评价Service业务层处理
 *
 * @author xy
 * @date 2024-10-11
 */
@Service
public class FeedbackEvaluationServiceImpl implements IFeedbackEvaluationService
{
    private static final Logger log = LoggerFactory.getLogger(FeedbackEvaluationServiceImpl.class);

    @Autowired
    private FeedbackEvaluationMapper feedbackEvaluationMapper;

    /**
     * 查询反馈评价
     *
     * @param id 反馈评价主键
     * @return 反馈评价
     */
    @Override
    public FeedbackEvaluation selectFeedbackEvaluationById(Long id)
    {
        return feedbackEvaluationMapper.selectFeedbackEvaluationById(id);
    }

    /**
     * 查询反馈评价列表
     *
     * @param feedbackEvaluation 反馈评价
     * @return 反馈评价
     */
    @Override
    public List<FeedbackEvaluation> selectFeedbackEvaluationList(FeedbackEvaluation feedbackEvaluation)
    {

        return feedbackEvaluationMapper.selectFeedbackEvaluationList(new DataScopeBase(),feedbackEvaluation);
    }

    /**
     * 新增反馈评价
     *
     * @param feedbackEvaluation 反馈评价
     * @return 结果
     */
    @Override
    public int insertFeedbackEvaluation(FeedbackEvaluation feedbackEvaluation)
    {
        feedbackEvaluation.setCreateTime(DateUtils.getNowDate());
        feedbackEvaluation.setTenantCode(SecurityUtils.getTenantCode());
        return feedbackEvaluationMapper.insertFeedbackEvaluation(feedbackEvaluation);
    }

    /**
     * 修改反馈评价
     *
     * @param feedbackEvaluation 反馈评价
     * @return 结果
     */
    @Override
    public int updateFeedbackEvaluation(FeedbackEvaluation feedbackEvaluation)
    {
        return feedbackEvaluationMapper.updateFeedbackEvaluation(feedbackEvaluation);
    }

    /**
     * 批量删除反馈评价
     *
     * @param ids 需要删除的反馈评价主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackEvaluationByIds(Long[] ids)
    {
        return feedbackEvaluationMapper.deleteFeedbackEvaluationByIds(ids);
    }

    /**
     * 删除反馈评价信息
     *
     * @param id 反馈评价主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackEvaluationById(Long id)
    {
        return feedbackEvaluationMapper.deleteFeedbackEvaluationById(id);
    }

    @Override
    public FeedbackEvaluation selectArchiveBorrowByBorrowId(Long id) {
        FeedbackEvaluation feedbackEvaluation = new FeedbackEvaluation();
        LoginUser user = SecurityUtils.getLoginUser();
        feedbackEvaluation.setDeptName(user.getSysUser().getDept().getDeptName());
        feedbackEvaluation.setFeedbackName(user.getSysUser().getNickName());
        List<Map<String, Object>> resultList = feedbackEvaluationMapper.selectYqDaByBorrowId(id);

        if (resultList != null && !resultList.isEmpty()) {
            StringBuilder daTitlesBuilder = new StringBuilder();
            for (Map<String, Object> map : resultList) {
                String subTableName = (String) map.get("SUB_TABLE_NAME");
                //达梦转换报错
//                Long daId = (Long) map.get("DA_ID");
//                if (daId == null && map.get("DA_ID") instanceof String) {
//                    daId = Long.parseLong((String) map.get("DA_ID"));
//                }
                String daId1 = String.valueOf(map.get("DA_ID"));
                Long daId = Long.parseLong(daId1);
                String param = subTableName != null && "x".equals(map.get("FEEDBACK")) ? "XMMC" : "TITLE";
                try {
                    String daTitle = feedbackEvaluationMapper.selectArchiveBorrowByBorrowId(subTableName, daId, param);
                    if (daTitle != null && !daTitle.trim().isEmpty()) {
                        if (daTitlesBuilder.length() > 0) {
                            daTitlesBuilder.append(", ");
                        }
                        daTitlesBuilder.append(daTitle);
                    }
                } catch (Exception e) {
                    // 异常处理：日志记录、用户提示等
                    log.error("feedbackEvaluationMapper.selectArchiveBorrowByBorrowId(subTableName, daId, param)查询报错" + "subTableName:"+subTableName+",daId:"+daId+",param:"+param, e);
                }
            }
            feedbackEvaluation.setDaTitle(daTitlesBuilder.toString());
        } else {
            log.error("feedbackEvaluationMapper.selectYqDaByBorrowId(id)返回结果集为空");
        }
        return feedbackEvaluation;
    }
}
